#include<cstdio>
#include<iostream>
#include<unistd.h>
#include<vector>
#include<string>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>


using namespace std;
vector<int> re;



int func()
{
    cout<<"进行拷贝"<<endl;
   string name=to_string(time(nullptr));
   name+=".date";
   FILE*fp=fopen(name.c_str(),"w");
   for(auto&e:re)
   {
       fputs(to_string(e).c_str(),fp);
       fputs(" ",fp);
   }
   return 0;
}


void save()
{
    pid_t id=fork();
    if(id==0)
    {
        int i=func();
        exit(i);
    }
    else
    {
        int status=0;
        pid_t cpid = waitpid(id,&status,0);
        cout<<"son pid="<<cpid<<"退出代码:"<<WIFEXITED(status)<<endl;
    }
}


int main()
{
    int cnt=1;
    while(true)
    {
        re.push_back(cnt);
        if(cnt%10==0)
        {
            save();
        }
        cnt++;
        sleep(1);
    }
    return 0;
}
